package com.wwdx.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wwdx.BaseCommons;
import com.wwdx.model.MerchantOrderModel;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.model.dto.PurchaseCordDTO;
import com.wwdx.service.UserServiceI;
import com.wwdx.utils.ConstantHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 * @date 2018/9/28 18:03
 */
@Service
public class UserServiceImpl extends BaseCommons implements UserServiceI{

    @Override
    public List<PurchaseCordDTO> bookshelfList(String openId,String merchantId){
        logger.info("用户ID:{} 商户ID:{}",openId,merchantId);
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("openId",openId);
        params.put("merchantId",merchantId);
        String hql = "from MerchantOrderModel where openId=:openId and merchantId=:merchantId and status = 1";
        List<MerchantOrderModel> orderModels = merchantOrderService.find(hql, params);
        List<PurchaseCordDTO> list = Lists.newArrayListWithExpectedSize(orderModels.size());
        orderModels.forEach(merchantOrderModel -> {
            Long productId = merchantOrderModel.getProductId();
            Integer type = merchantOrderModel.getType();
            PurchaseCordDTO purchaseCordDTO = PurchaseCordDTO.builder()
                    .isorder(1).productId(productId).payTime(new Date()).type(type).build();
            switch (type){
                //主题
                case ConstantHolder.Constant.THEMETYPE:
                    WwdxThemeModel wwdxThemeModel = wwdxThemeService.getByThemeId(productId);
                    purchaseCordDTO.setProductImg(wwdxThemeModel.getImg());
                    purchaseCordDTO.setProductName(wwdxThemeModel.getTitle());
                    String[] bookList = wwdxThemeModel.getBookList().split(",");
                    //书籍数量
                    purchaseCordDTO.setBookCount((long) bookList.length);
                    purchaseCordDTO.setProductIntroduce(wwdxThemeModel.getContentabstract());
                    break;
                //书籍 | 小课
                case ConstantHolder.Constant.BOOKTYPE:
                case ConstantHolder.Constant.SMALLBOOKTYPE:
                    WwdxBookModel wwdxBookModel = wwdxBookService.getByBookId(productId);
                    if (type == ConstantHolder.Constant.SMALLBOOKTYPE){
                        purchaseCordDTO.setBookChapterLength(wwdxBookChapterService.findBookChapterList(productId).size());
                    }
                    purchaseCordDTO.setProductImg(wwdxBookModel.getBookimg());
                    purchaseCordDTO.setProductName(wwdxBookModel.getBookname());
                    purchaseCordDTO.setVideoduration(wwdxBookModel.getVideoduration());
                    purchaseCordDTO.setProductIntroduce(wwdxBookModel.getBookrecommend());
                    break;
                default:
                    break;
            }
            list.add(purchaseCordDTO);
        });
        return list;
    }

    @Override
    public List<Object> bookshelfList(String openId){
        List<Object> objects = Lists.newArrayListWithExpectedSize(16);
        //已购买
        objects.addAll(hashOperations.values(String.format(ConstantHolder.USER_PURCHASE_MAP,openId)));
        //书架
        objects.addAll(hashOperations.values(String.format(ConstantHolder.USER_BOOKSHELF_MAP,openId)));
        return objects;
    }

    @Override
    public Integer isorder(Long productId, String openId,String merchantId){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(4);
        params.put("openId",openId);
        params.put("merchantId",merchantId);
        params.put("productId",productId);
        String hql = "from MerchantOrderModel where openId=:openId and merchantId=:merchantId and productId=:productId and status = 1";
        MerchantOrderModel merchantOrderModel = merchantOrderService.getByHql(hql, params);
        if (merchantOrderModel != null){
            return 1;
        }
        return 0;
    }

    @Override
    public Integer isbookshelf(Long productid,String unionid){
        String key = String.format(ConstantHolder.USER_BOOKSHELF_MAP,unionid);
        Boolean hexists = hashOperations.hasKey(key, productid.toString());
        if (hexists != null && hexists){
            return 1;
        }
        return 0;
    }

    @Override
    public void purchase(String openId,Long productId,Integer type){
        logger.info("用户:{} 购买的产品:{}",openId,productId);
        PurchaseCordDTO purchaseCordDTO = PurchaseCordDTO.builder()
                .isorder(1).productId(productId).payTime(new Date()).type(type).build();
        switch (type){
            //主题
            case ConstantHolder.Constant.THEMETYPE:
                WwdxThemeModel wwdxThemeModel = wwdxThemeService.getByThemeId(productId);
                purchaseCordDTO.setProductImg(wwdxThemeModel.getImg());
                purchaseCordDTO.setProductName(wwdxThemeModel.getTitle());
                purchaseCordDTO.setProductIntroduce(wwdxThemeModel.getContentabstract());
                break;
            //书籍 | 小课
            case ConstantHolder.Constant.BOOKTYPE:
            case ConstantHolder.Constant.SMALLBOOKTYPE:
                WwdxBookModel wwdxBookModel = wwdxBookService.getByBookId(productId);
                purchaseCordDTO.setProductImg(wwdxBookModel.getBookimg());
                purchaseCordDTO.setProductName(wwdxBookModel.getBookname());
                purchaseCordDTO.setProductIntroduce(wwdxBookModel.getBookrecommend());
                break;
            default:
                return;
        }
        String key = String.format(ConstantHolder.USER_PURCHASE_MAP,openId);
        hashOperations.put(key,productId.toString(),purchaseCordDTO);
    }
}